<?php
namespace Manager\Controller;
use Think\Upload;
use Think\Image;
/**
* ==============================================
* URL：http://www.liuhuanhui.cn
* ==============================================
* @date：2014-10-20
* @time：下午8:20:03
* @author：liuhuanhui
* @encoding：UTF-8
*/
class OptionsController extends  AuthController
{
	function  index()
	{
		$this->display();
	}
	/**
	 * 网站设置
	 */
	function  web()
	{
		$model=M('options');
		$arr=$model->select();
		$data['url']=$model->where('name="URL"')->getField('value');
		$data['title']=$model->where('name="WEBTITLE"')->getField('value');
		$data['isreg']=$model->where('name="ISREG"')->getField('value');
		$data['name']=$model->where('name="WEBNAME"')->getField('value');
		$data['icp']=$model->where('name="ICP"')->getField('value');
		$data['description']=$model->where('name="DESCRIPTION"')->getField('value');
		$data['keywords']=$model->where('name="KEYWORDS"')->getField('value');
		$data['webstatu']=$model->where('name="WEBSTATU"')->getField('value');
		$data['logincode']=$model->where('name="LOGINCODE"')->getField('value');
		$data['RUNDATE']=$model->where('name="RUNDATE"')->getField('value');
		$data['topText']=$model->where('name="topText"')->getField('value');
		$this->assign('data',$data);
		$this->display();
	}
	/**
	 * 处理网站设置
	 */
	function webHandle()
	{
		$_POST['ISREG']=I('ISREG','n');
		$_POST['LOGINCODE']=I('LOGINCODE','n');
		$_POST['WEBSTATU']=I('WEBSTATU','n');
		$this->updateInfo($_POST);
	}
	/**
	 * 调用保存
	 * @param unknown $arr
	 */
	function updateInfo($post)
	{
		$model=M('options');
		$data=array();
		foreach($post as $key=>$v)
		{
			$data[]=Array('name'=>$key,'value'=>$v);
		}
		if(M('options')->addAll($data,array(),true))
		{
			$this->success('保存网站信息成功！',U(web));
		}else{
			$this->error('保存网站信息失败！');
		}
	}
	/**
	 * 关于我
	 */
	function about()
	{
	
		$about=M('options')->field('name,value')->where(array(group=>1))->select();
		$data=array();
		foreach ($about as $a)
		$data[$a['name']]=$a['value'];
		
		$this->assign('about',$data);
		$this->display();
	}
	/**
	 * 修改密码
	 */
	function setpwd()
	{
		$this->display();
	}	
	/**
	 * 修改密码处理方法
	 */
	function setpwdHandle()
	{
		$user=session('auth');
		if(md5(I('oldpwd'))!=$user['userpassword'])
		{
			$this->error("当前登录密码错误~！");
		}
		if(I('pwd1')!=I('pwd2'))
		{
			$this->error("两次密码不相符合！");
		}
		$user['userpassword']=I('pwd1','','md5');
		if(M('users')->save($user))
		{
			session_unset();
			session_destroy();
			$this->success('修改新的密码成功,请使用新密码登录！',U('Login/index'));
		}else{
			$this->error("修改密码失败！");
		}
	}
	
	/**
	 * 关于我的处理方法
	 */
	function aboutHandle()
	{
	
		if(!empty($_FILES['file']['name']))
		{	
			/********SAE*******/
			
			$upload=new Upload();
			$upload->savePath='/system_images/';
			$upload->saveName='about_me_show';
			$upload->autoSub=false;
			$upload->replace=true;
			$info=$upload->uploadOne($_FILES['file']);

		$fileName='about_me_show.'.pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
		//生成缩略图
		$s = new \SaeStorage();
		$f = new \SaeFetchurl();
		$img= new \SaeImage();
		$first_img_pic= $info['url'];
		$img_data= $f->fetch( $first_img_pic);
		$img->setData($img_data);
		//$img->resize(400);// 等比缩放
		$Smallimg= $img->exec(); // 执行处理并返回处理后的二进制数据
		$s->write('uploads',"system_images/".$fileName,$Smallimg, -1);
		if(!$info||!$s)
		{
			$this->error('照片Show上传失败！');
		}
		
		$_POST['about_SHOW']=$fileName;
			
			/* linux and  Windows
				$fileName='about_me_show.'.pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
				$savePath='uploads/system_images/'.$fileName;
				$image = new Image();
				$image->open($_FILES['file']['tmp_name']);
				$result=$image->thumb(150,200)->save($savePath,null,200);
				*/
		}
		$_POST['about_BORNDATE']=I('about_BORNDATE','','strtotime');
		$data=array();
		foreach ($_POST as $k=>$v)
			$data[]=array(name=>$k,value=>$v,group=>1);
		if(M('options')->addAll($data,array(),true))
		{
			$this->success('保存关于我的信息成功！');
		}else{
			$this->error('保存关于我的信息失败！');
		}
	}


}